package com.jingnen.guess_index_server.service;

import java.time.LocalDateTime;
import java.util.Optional;

import org.springframework.stereotype.Service;

import com.jingnen.guess_index_server.entity.UserAuth;
import com.jingnen.guess_index_server.repository.UserAuthRepository;

@Service
public class UserAuthServiceImpl implements UserAuthService {
    
    private final UserAuthRepository userAuthRepository;

    public UserAuthServiceImpl(UserAuthRepository userAuthRepository) {
        this.userAuthRepository = userAuthRepository;
    }

    @Override
    public UserAuth saveUserAuth(UserAuth userAuth) {
        if (userAuth.getLoginTime() == null) {
            userAuth.setLoginTime(LocalDateTime.now());
        }

        if (userAuth.getCreateTime() == null) {
            userAuth.setCreateTime(LocalDateTime.now());
        }

        return userAuthRepository.save(userAuth);
    }

    @Override
    public Optional<UserAuth> getUserAuthByUserIdAndLoginType(Long userId, String loginType) {
        return userAuthRepository.findByUserIdAndLoginType(userId, loginType);
    }
}
